package com.zhiyou.controller;

import java.io.File;
import java.util.Random;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.zhiyou.pojo.User;
import com.zhiyou.service.UserService;
import com.zhiyou.tools.FileUploadUtil;
import com.zhiyou.tools.ImgCut;
import com.zhiyou.tools.JavaEmailSender;
import com.zhiyou.tools.MD5Utils;
import com.zhiyou.tools.RandomCode;
import com.zhiyou.tools.SentEmail;


@Controller
//@RequestMapping("user")
public class UserController {

	

	/*@RequestMapping("/index.action")
	public String showIndex(){
		return "before/index";
	}*/
	@Autowired
	private UserService userService;
	@Value("${HEAD_IMG}")
	public String HEAD_IMG;
	@Value("${IMG_SEARVER_URL}")
	public String IMG_SEARVER_URL;
	@RequestMapping("/validateEmail.action")
	@ResponseBody
	public String validateEmail(String email){
		
		System.out.println("validateEmail:"+email);
		return userService.validateEmail(email);
		
	}
	
	@RequestMapping("/insertUser.action")
	@ResponseBody
	public String insertUser(User user,HttpSession session){
		
		
		user.setPassword(MD5Utils.getMd5(user.getPassword()));
		
		int result =userService.insertUser(user);
		if(result>0){
			session.setAttribute("userAccount", user.getEmail());
			return "success";
		}
		return "fail";
		
		
	}
	
	@RequestMapping("/loginUser.action")
	@ResponseBody
	public String login(User user,HttpSession session){
		
		
		
		user.setPassword(MD5Utils.getMd5(user.getPassword()));
		
		int count =userService.login(user);
		if(count>0){
			session.setAttribute("userAccount", user.getEmail());
			return "success";
		}
		return "fail";
	}
	
	
	@RequestMapping("/loginOut.action")
	@ResponseBody
	public void loginOut(HttpSession session){
		
			session.removeAttribute("userAccount");
			
	}
	@RequestMapping("/loginOut2.action")
	
	public String loginOut2(HttpSession session){
		
			session.removeAttribute("userAccount");
		return "before/index.action";
	}
	@RequestMapping("/myProfile.action")
	public String showMyProfile(HttpSession session,Model model){
		
		String email =  (String) session.getAttribute("userAccount");
		User user =userService.selectUserByEmail(email);
		model.addAttribute("user", user);
		return "before/my_profile";	
	}
	
	@RequestMapping("/changeProfile.action")
	public String changeProfile(HttpSession session,Model model){
		
		String email =  (String) session.getAttribute("userAccount");
		User user =userService.selectUserByEmail(email);
		model.addAttribute("user", user);
		return "before/change_profile";	
	}
	
	
	@RequestMapping("/updateUser.action")
	public String updateUser(User user,Model model){
		
		userService.updateUserById(user);
		
		return "redirect:/showMyProfile.action";	
	}
	
	@RequestMapping("/passwordSafe.action")
	public String passwordSafe(HttpSession session,Model model){
		String email =  (String) session.getAttribute("userAccount");
		User user =userService.selectUserByEmail(email);
		model.addAttribute("user", user);
		return "before/password_safe";	
	}
	
	
	@RequestMapping("validatePassword.action")
	@ResponseBody
	public String vaildatePassword(String password,HttpSession session){
		
		String account =  (String) session.getAttribute("userAccount");
		String md5Pass= MD5Utils.getMd5(password);
		
		return userService.validateMd5Pass(md5Pass,account);
	}
	
	
	@RequestMapping("updatePassword.action")
	public String updatePassword(String newPassword,HttpSession session){
		
		String account =  (String) session.getAttribute("userAccount");
		String md5Pass= MD5Utils.getMd5(newPassword);
		
		userService.updateMd5Pass(md5Pass,account);
		return "redirect:/myProfile.action";	
		
	}
	
	
	//忘记密码
	@RequestMapping("forgetPassword.action")
	public String forgetPassword(){
		return "before/forget_password";
	}
	
	/*@RequestMapping("eachpassword")
	@ResponseBody
	public String eachByEmail(String email,HttpSession session){
		String result=userService.eachPassword(email);
		session.setAttribute("email",email);
		if(null != result && result.length()>0){
			
			SentEmail.sendAndCc("smtp.163.com", "m15239261761_1@163.com", 
					email, "", "查找密码邮件", result, "m15239261761_1@163.com", "wenxiaohui1993");
			return "success";
		}else {
			return "fail";
		}
	}*/

   @RequestMapping("sendEmail.action")
   public String sendEmail(String email) throws Exception{
	   int result = userService.countUserByEmail(email);
	   if (result>0) {
		 String code =RandomCode.getRandomCode(6);
		 /*JavaEmailSender.sendEmail(email, "找回密码", code);*/
		 SentEmail.sendAndCc("smtp.163.com", "m15239261761_1@163.com", 
					email, "", "查找密码邮件", code, "m15239261761_1@163.com", "wenxiaohui1993");
		 userService.savaCodeByEmail(code,email);
		 return "success";
	}else {
		return "notExist";
	}
   }
   @RequestMapping("validateEmailCode.action")
	public String validateEmailCode(String email,String code,Model model) throws Exception{
		
		//通过账号获取数据
		int result =userService.validateEmailCode(code,email);
		if(result>0){
			model.addAttribute("email", email);
			return "before/reset_password";
		}else{
			return "before/forget_password";
		}
		
		
	}
	
	@RequestMapping("resetPassword.action")
	public String resetPassword(String email,String password) throws Exception{
		
		//通过账号获取数据
		String md5Pass= MD5Utils.getMd5(password);
		userService.updateMd5Pass(md5Pass, email);
		return "redirect:/index.action";
		
		
	}
	//上传图片
	/*@RequestMapping("/UploadDemo")*/
	@RequestMapping("changeAvatar.action")
	public String changeAvatar(HttpSession session,Model model) throws Exception{
		
		String email =  (String) session.getAttribute("userAccount");
		User user =userService.selectUserByEmail(email);
		model.addAttribute("user", user);
		return "before/change_avatar";
		
		
	}
    
	@RequestMapping("upLoadImage.action")
	public String upLoadImage(HttpServletRequest request,MultipartFile image_file,HttpSession session,Model model) throws Exception{
		float x1 =Float.parseFloat(request.getParameter("x1"));
		float x2 =Float.parseFloat(request.getParameter("x2"));
		float y1 =Float.parseFloat(request.getParameter("y1"));
		float y2 =Float.parseFloat(request.getParameter("y2"));
		//需要知道image_file的后缀名是什么？
		String oldName=image_file.getOriginalFilename();
		String suffix= oldName.substring(oldName.lastIndexOf("."));
		System.out.println(suffix);
		//给图片生成一个新的名字
		String uuid= UUID.randomUUID().toString();
		String newName=uuid+suffix;
		//调用一个方法就OK
		image_file.transferTo(new File(HEAD_IMG+newName));
		//裁剪图片
		ImgCut.cutImage(HEAD_IMG+newName, (int)x1,(int)y1, (int)(x2-x1), (int)(y2-y1));
		//修改数据库中的url
		
		String email =  (String) session.getAttribute("userAccount");
		userService.updateImgUrlByEmail(IMG_SEARVER_URL+newName,email);
		User user = new User();
		user.setImgurl(IMG_SEARVER_URL+newName);
		model.addAttribute("user", user);
		return "before/change_avatar";
		
		
	}
	   
	
	
}
